package com.dz.scrm.mvp.ui

import android.graphics.Typeface
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dz.scrm.R
import com.dz.scrm.app.config.WHAT_CHECK_VERSION
import com.dz.scrm.constant.INTENT_PHONE_RECORD
import com.dz.scrm.mvp.model.entity.BaseResponse
import com.dz.scrm.mvp.model.entity.CachePhoneRecord
import com.dz.scrm.mvp.model.entity.CheckVersionBean
import com.dz.scrm.mvp.model.entity.CompanyInfoBean
import com.dz.scrm.mvp.presenter.DZPresent
import com.dz.scrm.utils.*
import com.miui.enterprise.sdk.PhoneManager
import com.tbruyelle.rxpermissions2.RxPermissions
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding
import io.reactivex.Observable
import io.reactivex.Observable.just
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_phone_detail.*
import kotlinx.android.synthetic.main.activity_phone_detail.iv_user_avatar
import kotlinx.android.synthetic.main.activity_phone_detail.tv_phone_num
import kotlinx.android.synthetic.main.fragment_mine.*
import me.jessyan.art.mvp.IView
import me.jessyan.art.mvp.Message
import me.jessyan.art.utils.ArtUtils
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import org.litepal.LitePal
import org.litepal.extension.find

/**
 * 作者：fanda
 * 日期：2021/4/23 15:02
 * 描述：电话详情界面
 */
class PhoneDetailActivity : DZActivity<DZPresent>(), IView {
    private lateinit var mCallDetailAdapter: CallDetailAdapter

    private val mPhoneRecord by lazy {
        intent.getSerializableExtra(INTENT_PHONE_RECORD) as CachePhoneRecord
    }

    override fun initView(savedInstanceState: Bundle?) = R.layout.activity_phone_detail

    override fun initData(savedInstanceState: Bundle?) {
        initPhoneNum()
        initRecyclerView()
        initListeners()
    }

    private fun initPhoneNum() {
        tv_phone_num.typeface = Typeface.defaultFromStyle(Typeface.BOLD)//加粗
        if (mPhoneRecord.userName.isEmpty()) {
            var number = mPhoneRecord.customerPhone
            if(number.length > 11){
                number = PhoneManager.getInstance().decodePhoneNumber(number)
            }

            if (!isHiddenPhoneNum()) {
                tv_phone_num.text = getFormatPhoneNum(number)
            } else {
                tv_phone_num.text = getEncryptPhoneNum(number)
            }
        } else {
            tv_phone_num.text = mPhoneRecord.userName
        }
    }

    private fun initRecyclerView() {
        mCallDetailAdapter = CallDetailAdapter()
        call_detail_recyclerView.adapter = mCallDetailAdapter
        call_detail_recyclerView.layoutManager = LinearLayoutManager(this)

        val cachePhoneRecordList = LitePal.where("user_id = ? and tenant_id = ? and customerPhone = ?", getUserId(), getTenantId(), mPhoneRecord.customerPhone).find<CachePhoneRecord>().reversed()
        mCallDetailAdapter.setNewInstance(cachePhoneRecordList.toMutableList())
    }

    private fun initListeners() {
        iv_back.setOnClickListener { finish() }
    }

    override fun obtainPresenter() = DZPresent(ArtUtils.obtainAppComponentFromContext(this), RxPermissions(this))

    override fun handleMessage(message: Message) {
        when (message.what) {
        }
    }

    override fun showMessage(message: String) {
        ArtUtils.snackbarText(message)
    }
}

/**
 * 作者：fanda
 * 描述：未接电话
 */
class CallDetailAdapter : BaseQuickAdapter<CachePhoneRecord, BaseViewHolder>(R.layout.item_call_detail) {

    override fun convert(holder: BaseViewHolder, item: CachePhoneRecord) {

        holder.setText(R.id.tv_phone_date, getPhoneDate(item.phoneStartTime))
        val stateBuilder = StringBuilder()
        if (item.isInCall()) {
            stateBuilder.append("呼入")
        } else {
            stateBuilder.append("呼出")
        }

        if (item.isConnect()) {
            stateBuilder.append("接通")
        } else {
            stateBuilder.append("未接")
        }

        holder.setText(R.id.tv_phone_state, stateBuilder.toString())

        if (item.isDisagree) {
            // 拒接，显示响铃秒数
            holder.setText(R.id.tv_phone_duration, "响铃${AppUtils.secToTime((item.ringDurationTime / 1000).toInt())}")
        } else {
            if (item.isConnect()) {
                // 显示通话时间
                holder.setText(R.id.tv_phone_duration, AppUtils.secToTime((item.phoneDurationTime)))
            } else {
                // 显示未接通
                holder.setText(R.id.tv_phone_duration, "未接通")
            }
        }
    }
}